package com.terrace.web.tagext.easyui;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.terrace.web.constant.EasyuiConstant;
import com.terrace.web.tagext.easyui.support.ComboTagSupport;
import com.terrace.web.tagext.easyui.utils.EasyUIUtils;

/**
 * 组合框
 * @author jiangyg
 *
 */
public class ComboBoxTag extends ComboTagSupport {
	
	private static final long serialVersionUID = -5188626663177244256L;

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ComboBoxTag.class);

	/**
	 * 组合框显示字段
	 */
	private String textField = "dataName";
	
	/**
	 * 组合框值字段
	 */
	private String valueField = "dataValue";
	
	/**
	 * 数据字典
	 */
	private String dictionary;

	@Override
	public int doStartTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			StringBuffer sb = new StringBuffer();
			
			// 字典代码不为空
			if (StringUtils.isNotBlank(dictionary)) {
				sb.append("<input id=\"" + name + "\" name=\"" + name + "\" type=\"text\" class=\"easyui-combobox\" ");
				sb.append("data-options=\"" + assembleDataOptions() + ",");
				EasyUIUtils.appendDatsOption(sb, "valueField", valueField);
				EasyUIUtils.appendDatsOption(sb, "textField", textField);
				EasyUIUtils.appendDatsOption(sb, "url", EasyuiConstant.message("data.dict.code.url", dictionary));
				EasyUIUtils.substringLastComma(sb);
				sb.append("\"");
				sb.append(">");
			}
			
			sb.append(this.setComboValue());
			
			out.print(sb.toString());
		} catch (Exception e) {
			logger.error("数据字典组合框生成失败：", e);
		}
		return EVAL_PAGE;
	}

	public String getDictionary() {
		return dictionary;
	}

	public void setDictionary(String dictionary) {
		this.dictionary = dictionary;
	}

}
